package com.ostrich.cloud.ostrichOrderService.component;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.isuyu.boot.oss.starter.autoconfigure.service.OssService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@Component
public class QRcodeUtils {
    @Resource
    private OssService ossService;



    public static BufferedImage generateQrCode(String content) {
        BufferedImage image = QrCodeUtil.generate(
                content, // 二维码内容
                QrConfig.create()// 附带logo
                        .setWidth(490) // 二维码的宽
                        .setHeight(490) // 二维码的高
                        .setMargin(0)); // 边距
        return image;
    }

    public static BufferedImage createPictureNew(BufferedImage image, String bgPath, String text) {
        try {
            // 首先先画背景图片
            BufferedImage backgroundImage = ImageIO.read(new File(bgPath));
            // 背景图片的宽度
            int bgWidth = backgroundImage.getWidth();
            // 二维码的宽度
            int qrWidth = image.getWidth();
            // 二维码距离背景图片横坐标（X）的距离，居中显示
            int distanceX = (bgWidth - qrWidth) / 2;
            // 二维码距离背景图片纵坐标（Y）的距离
            int distanceY = distanceX;
            // 基于图片backgroundImage对象打开绘图
            Graphics2D rng = backgroundImage.createGraphics();
            rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
            rng.drawImage(image, distanceX, distanceY, null);

            //设置字体
            Font font = new Font("宋体", Font.PLAIN, 30);
            rng.setFont(font);
            rng.setColor(Color.black);
            // 获取当前文字的对象
            FontMetrics metrics = rng.getFontMetrics(font);

            // 文字在图片中的坐标 这里设置在中间
            int startX = (bgWidth - metrics.stringWidth(text)) / 2; //当前文字对象到横坐标（X）的距离
            int startY = backgroundImage.getHeight()-10; //当前文字对象到纵坐标（Y）的距离
            rng.drawString(text, startX, startY);
            // 处理绘图
            rng.dispose();
            image = backgroundImage;
            image.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将绘制好的图片返回
        return image;
    }

    public static InputStream getImageStream(BufferedImage bi) {

        InputStream is = null;

        ByteArrayOutputStream bs = new ByteArrayOutputStream();

        ImageOutputStream imOut;
        try {
            imOut = ImageIO.createImageOutputStream(bs);

            ImageIO.write(bi, "png", imOut);

            is = new ByteArrayInputStream(bs.toByteArray());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }


    public String getQrCode(Long orederNo){

        // 存放在二维码中的内容
        String text ="http://192.168.140.87:8999/api/ostrichUserWeb/customerSearch?orderNo="+orederNo;

        //生成二维码
        BufferedImage qrCodeImage =generateQrCode(text);
        InputStream in = getImageStream(qrCodeImage);


        String upload = ossService.upload(orederNo+".jpg", in);

        return upload;
    }
}
